﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dmp_ui_demo
{
    public delegate void EditorDataSavedHandler(string identity, string title, string content);
    /// <summary>
    /// 用户在编辑器的WebBrowser对象和程序之间相互调用的类
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public class BrowserBridge
    {
        /// <summary>
        /// 获取是否调试模式
        /// </summary>
        public bool IsDebug { get; private set; }

        /// <summary>
        /// 获取浏览器对象
        /// </summary>
        public WebBrowser Browser { get; private set; }

        /// <summary>
        /// 获取编辑器是否只读
        /// </summary>
        public bool ReadOnly { get; private set; }

        private string templateUrl = string.Empty;

        public event EditorDataSavedHandler OnDataSave;

        public BrowserBridge(WebBrowser browser, bool readOnly, string templateUrl)
        {
            this.Browser = browser;
            this.Browser.ObjectForScripting = this;
            this.Browser.DocumentCompleted += Browser_DocumentCompleted;
            this.ReadOnly = readOnly;
            this.templateUrl = templateUrl;
#if DEBUG
            IsDebug = true;
            this.templateUrl = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\CkeditorPlugins\PackageInsert.htm"));
#endif
            loadTemplate();
        }

        /// <summary>
        /// 刷新编辑器
        /// </summary>
        public void Refresh()
        {
            loadTemplate();
        }

        void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                AddParagraph("identity" + i, "标题" + i, "内容" + i);
            }
        }

        public void Goto(string identity)
        {
            Browser.Document.InvokeScript("window.location.hash = '" + identity + "';");
            //Browser.Url = new Uri(templateUrl + "#" + identity);
        }

        private void loadTemplate()
        {
            Browser.Url = new Uri(templateUrl);
        }

        /// <summary>
        /// 想编辑器添加一个编辑段落
        /// </summary>
        /// <param name="identity">段落标识符</param>
        /// <param name="title">段落标题</param>
        /// <param name="content">段落内容</param>
        public void AddParagraph(string identity, string title, string content)
        {
            var element = Browser.Document.CreateElement("div");
            element.Style = "height:100px;";
            element.InnerHtml = "<a name=\"" + identity + "\">" + identity + "</a>";
            Browser.Document.Body.AppendChild(element);
        }

        /// <summary>
        /// 保存编辑器中未保存的段落
        /// </summary>
        /// <param name="destroy">是否销毁编辑器，默认为false</param>
        public void SaveData(bool destroy = false)
        {
            Browser.Document.InvokeScript("GetEditStatus", new object[] { destroy });
        }

        /// <summary>
        /// 提供给Browser使用的方法
        /// </summary>
        public void DataSave(string identity, string title, string content)
        {
            OnDataSave(identity, title, content);
        }
    }
}
